package com.jimikongjian.service.dao.impl;

import com.jimikongjian.dao.IRoom;
import com.jimikongjian.models.Room;
import com.jimikongjian.service.MailService;
import com.jimikongjian.service.modal.FollowerRoomInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fuxiuyang on 17/2/26.
 */
@Service("iRoomImpl")
public class IRoomImpl implements IRoom {

    @Autowired
    @Qualifier("mailService")
    private MailService mailService;

    @Autowired
//    @Qualifier("roomMapper")
    private IRoom roomMapper;

    /**
     * 获取所有的空间信息
     *
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getRoomList(int start, int length) {
        if (start >= 0 && length > 0){
            return roomMapper.getRoomList(start, length);
        }
        return null;
    }

    /**
     * 通过空间的id，获取该空间的信息
     *
     * @param id
     * @return List<Room>
     */
    public List<Room> getRoomListById(int id) {
        if (id > 0){
            return roomMapper.getRoomListById(id);
        }
        return null;
    }

    /**
     * 通过空间的id，获取该空间的信息
     *
     * @param id
     * @return Room
     */
    public Room getRoomById(int id) {
        List<Room> rooms = getRoomListById(id);
        if (rooms.size() > 0){
            return rooms.get(0);
        }
        return null;
    }

    /**
     * 通过创建者的id，获取其创建的所有的空间
     *
     * @param creatorId
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getRoomListByCreatorId(int creatorId, int start, int length) {
        if(creatorId > 0 && start >= 0 && length > 0){
            return roomMapper.getRoomListByCreatorId(creatorId, start, length);
        }
        return null;
    }

    /**
     * 通过创建者的id，获取其创建的个人的空间
     *
     * @param creatorId
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getMyRoomListByCreatorId(int creatorId, int start, int length) {
        if (creatorId > 0 && start >= 0 && length > 0){
            return roomMapper.getMyRoomListByCreatorId(creatorId, start, length);
        }
        return null;
    }

    /**
     * 通过创建者的id，获取其创建的私人的空间
     *
     * @param creatorId
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getPrivateRoomListByCreatorId(int creatorId, int start, int length) {
        if (creatorId > 0 && start >= 0 && length >0){
            return roomMapper.getPrivateRoomListByCreatorId(creatorId, start, length);
        }
        return null;
    }

    /**
     * 通过创建者的id，获取其创建的公共的空间
     *
     * @param creatorId
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getPublicRoomListByCreatorId(int creatorId, int start, int length) {
        if (creatorId > 0 && start >= 0 && length > 0){
            return roomMapper.getPublicRoomListByCreatorId(creatorId, start, length);
        }
        return null;
    }

    /**
     * 通过空间名，叫该空间名的或者包含该名称的所有的空间
     *
     * @param name
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getRoomListByName(String name, int start, int length) {
        if (start >= 0 && length > 0){
            return roomMapper.getRoomListByName(name, start, length);
        }
        return null;
    }

    /**
     * 通过空间名，叫该空间名的或者包含该名称的所有个人的空间
     *
     * @param name
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getMyRoomListByName(String name, int start, int length) {
        if (start >= 0 && length > 0){
            return roomMapper.getMyRoomListByName(name, start, length);
        }
        return null;
    }

    /**
     * 通过空间名，叫该空间名的或者包含该名称的所有私人的空间
     *
     * @param name
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getPrivateRoomListByName(String name, int start, int length) {
        if (start >= 0 && length >0){
            return roomMapper.getPrivateRoomListByName(name, start, length);
        }
        return null;
    }

    /**
     * 通过空间名，叫该空间名的或者包含该名称的所有公有的空间
     *
     * @param name
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getPublicRoomListByName(String name, int start, int length) {
        if (start >= 0 && length > 0){
            return roomMapper.getPublicRoomListByName(name, start, length);
        }
        return null;
    }

    /**
     * 通过用户的id号，获取该用户关注的所有的空间信息
     *
     * @param userId
     * @return List<Room>
     */
    @Override
    public List<FollowerRoomInfo> getAllRoomListByUserId(int userId) {
        if (userId > 0){
            return roomMapper.getAllRoomListByUserId(userId);
        }
        return null;
    }

    /**
     * 通过用户的id号，获取该用户关注的所有的空间信息
     *
     * @param userId
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getRoomListByUserId(int userId, int start, int length) {
        if (userId > 0 && start >= 0 && length > 0){
            return roomMapper.getRoomListByUserId(userId, start, length);
        }
        return null;
    }

    /**
     * 通过用户的id号，获取该用户关注自己的空间信息
     *
     * @param userId
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getMyRoomListByUserId(int userId, int start, int length) {
        if (userId > 0 && start >= 0 && length > 0){
            return roomMapper.getMyRoomListByUserId(userId, start, length);
        }
        return null;
    }

    /**
     * 通过用户的id号，获取该用户关注的所有的私有的空间信息
     *
     * @param userId
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getPrivateRoomListByUserId(int userId, int start, int length) {
        if (userId > 0 && start >= 0 && length > 0){
            roomMapper.getPrivateRoomListByUserId(userId, start, length);
        }
        return null;
    }

    /**
     * 通过用户的id号，获取该用户关注的所有的公有的空间信息
     *
     * @param userId
     * @param start
     * @param length
     * @return List<Room>
     */
    public List<Room> getPublicRoomListByUserId(int userId, Integer start, Integer length) {
        if (userId > 0 && start == -99 && length == -99){
            roomMapper.getPublicRoomListByUserId(userId, null, null);
        }
        if (userId > 0 && start >= 0 && length > 0){
            roomMapper.getPublicRoomListByUserId(userId, start, length);
        }
        return null;
    }

    /**
     * 创建一条新的空间
     *
     * @param room
     */
    public void insertRoom(Room room) {
        if (room.getName() != "" && room.getCover() != ""
                && room.getCreatorId() > 0 && mailService.isEmail(room.getCreatorEmail())
                && room.getCreatorNick() != ""){
            roomMapper.insertRoom(room);
            System.out.println(room.getId());
//            return room.getId()
        }
//        return null;
    }

    /**
     * 更新空间信息,只有空间的名称，空间的封面，空间的个性签名，空间的标签可以更改
     *
     * @param room
     */
    public void updateRoom(Room room) {
        if (room.getId() > 0 ){
            roomMapper.updateRoom(room);
        }
    }

    /**
     * 将空间的关注用户+1
     *
     * @param id
     */
    @Override
    public void updateAddPeoplesCount(int id) {
        if (id > 0){
            roomMapper.updateAddPeoplesCount(id);
        }
    }

    /**
     * 将空间的关注用户-1
     *
     * @param id
     */
    @Override
    public void updateSubPeoplesCount(int id) {
        if (id > 0){
            roomMapper.updateSubPeoplesCount(id);
        }
    }

    /**
     * 删除空间信息
     *
     * @param room
     */
    public void deleteRoom(Room room) {
        if (room.getId() > 0){
            roomMapper.deleteRoom(room);
        }
    }
}
